#include<stdio.h>
//#include "../include/linkedlist.h"
#include "../include/filevfs.h"
#include "../include/createfile.h"
#include "../include/narryTree.h"
#include "../include/addfile.h"

int main()
{
	
	long int size;
	int i=1,j,k,l,m,x,y,countInput;
	
	char a[50];
	char n,name[20],name1[20],location[30];
	int flag1,flag2,mountflag,fileWrittenFlag;
	flag1=1;
	mountflag=0;
	struct node *root;
	struct fileDescriptor *fd1;
	char option[20],input1[50],input[50],input2[50],fsname[20];
//-----------------------------------------------------------------------------------------------------------------------
//creating options and implementation of commands
	while(1)
	{
		countInput=0;
		printf("\n->$");
		//scanf("%s %s %s",option,input1);
		gets(input);				//get input from user
		m=strlen(input);			//get length of input
		j=0;
		k=0;

							//code for parsing the input into different fields
					
		if(m>0)
		{					
		while (input[j] != ' '&& j<m)		
		{
			option[k]=input[j];		//1st field of input copied to option
			j++;k++;
			l=j;
		}
		countInput++;				//keep count of number of input parameterss
		option[k]='\0';
		printf("value input1:%s\n",option);
		}
		l++;
		if(l<=m)
		{
		k=0;
		//l++;
		while (input[l] != ' ' && l<m)		
		{
			input1[k]=input[l];		//2nd field of input copied to input1
			l++;k++;
			x=l;
		}
		input1[k]='\0';
		printf("value input2:%s\n",input1);
		countInput++;
		}
		x++;
		if(x<=m)
		{
		k=0;
		while (input[x] != ' ' && x<m)		//3rd field of input copied to input2
		{
			input2[k]=input[x];
			x++;k++;
						
		}
		input2[k]='\0';
		printf("value input3:%s\n",input2);
		countInput++;
		}
		while(x<m)					//if more inputs then command invalid
		{	if(input[x] != ' ')
			{
				countInput++;				
				
			}
			x++;
		}

//-------------------------------------------------------------------------------------------------------------------
//if option is create vfs ,then call the create vfs method

		if((strcmp("create_vfs",option)==0)&&countInput==2&&flag1)
		{
		flag1=0;							//one time creation,so set flag1=0
		//printf("Enter file system label :");
		//scanf("%s",name);
		strcpy(fsname,input1);
		printf("Enter size of file system interms of kilo bytes(KB):");
		scanf("%ld",&size);	
		createFileVfs(input1,size);
		}
//-------------------------------------------------------------------------------------------------------------------
//if option is mount ,then call mount methods(narray,hash table,binary search tree)

		else if(strcmp("mount_vfs",option)==0&&countInput==2)
		{
			if(mountflag==0)
			{
			mountflag=1;
			root=narraymount(input1);
			if(root!=NULL)
			printf("File System Mounted!!!");
			strcpy(fsname,input1);
			//print(input1);
			}
			
			else 
			{
			printf("Multiple filesystems cannot be mounted simultaneously\n");
			}
		}
//-------------------------------------------------------------------------------------------------------------------
//if option is make directory,then call make directory method

		else if((strcmp("makedir",option)==0)&&countInput==3)
		{
			 if(mountflag==0)
			{
				 printf("File system Not mounted ,Try again !!!");
		       		 m=0;
			}
			printf("In main:%s\n",input1);
			makedircall(input2,input1,root);
			printfolder(root);
		}

//-------------------------------------------------------------------------------------------------------------------
//if option is delete directory,then call delete directory method

		else if((strcmp("deletedir",option)==0)&&countInput==2)
		{
			 if(mountflag==0)
			{
				 printf("File system Not mounted ,Try again !!!");
		       		 m=0;
			}
			root=deleteChild(input1,root);
			//printfolder(root);
		}
//-------------------------------------------------------------------------------------------------------------------

else if((strcmp("print",option)==0)&&countInput==2)
		{
			//reinitializefd(fsname);
			 if(mountflag==0)
			{
				 printf("File system Not mounted ,Try again !!!");
		       		 m=0;
			}
			
			print(input1);
		}

//-------------------------------------------------------------------------------------------------------------------

else if((strcmp("printhash",option)==0)&&countInput==1)
		{
			
			 if(mountflag==0)
			{
				 printf("File system Not mounted ,Try again !!!");
		       		 m=0;
			}
			
			printHash();
		}

//-------------------------------------------------------------------------------------------------------------------

else if((strcmp("printbst",option)==0)&&countInput==1)
		{
			
			 if(mountflag==0)
			{
				 printf("File system Not mounted ,Try again !!!");
		       		 m=0;
			}
			
			displayList();
		}
//-------------------------------------------------------------------------------------------------------------------
//if option is create file,then call data file creation method and then insert the file into narray tree

		else if((strcmp("create_file",option)==0)&&countInput==3)
		{
			if(mountflag==0)
			{
				 printf("File system Not mounted ,Try again !!!");
		       		 break;
			}
			else
			{
				fd1=createDataFile(input2,fsname,input1);
				if(fd1==NULL)
				{
					printf("Error in writing file !!...fileDescriptors full\n");
				}
				else
				{
					printf("type:%s\n",fd1->fileType);
					root=insertChild(input2,input1,root,fd1);	
					printfolder(root);
					
				}	
			}				
		}
//-------------------------------------------------------------------------------------------------------------------
//if option is add file,then call data file creation method and then insert the file into narray tree

		else if((strcmp("addfile",option)==0)&&countInput==3)
		{
			if(mountflag==0)
			{
				 printf("File system Not mounted ,Try again !!!");
		       		 break;
			}
			else
			{
				printf("in addfile call\n");
				fd1=addDataFile(input2,fsname,input1);
				if(fd1==NULL)
				{
					printf("Error in writing file !!...fileDescriptors full\n");
				}
				else
				{
					printf("type:%s\n",fd1->fileType);
					root=insertChild(input2,input1,root,fd1);	
					printfolder(root);
					
				}	
			}				
		}
//-------------------------------------------------------------------------------------------------------------------
//if option is displayfile ,then call displayfiles method !

		else if(strcmp("displayfiles",option)==0&&countInput==1)
				{
					displayfiles(fsname);
				}
//-------------------------------------------------------------------------------------------------------------------
//if option is exit ,then exit from app !

		else if(strcmp("exit",option)==0&&countInput==1)
		{
			exit(0);
		}
//-------------------------------------------------------------------------------------------------------------------
//if option is unmount,then call unmount method

		else if(strcmp("unmount_vfs",option)==0&&countInput==2)
		{
			mountflag=0;
			reinitializefd(fsname);
			unmount_narry(input1,root);
			exit(0);
			printf("File System UnMounted!!!");
		}



//-------------------------------------------------------------------------------------------------------------------
//if option is invalid,then display invalid command

		else
		{			
			printf("Invalid Command\n");
			m=0;
		}

	}
		
		
	return 0;
}
